package chu.dejian.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;

import java.util.ArrayList;
import java.util.List;

import chu.dejian.entity.MeasureMessageEntity;
import chu.dejian.entity.RecordEntity;

public class MeasureMessageDao {
	public static final String MESSAGE_TABLE = "measure_message";
	public static final String KEY_MESSAGE_ID = "messageId";
	public static final String KEY_ATTENDEE = "attendee";
	public static final String KEY_ATTENDEE_UID = "attendee_uid";
	public static final String KEY_MESSAGE = "message";
	public static final String KEY_SYSTOLIC = "systolic";
	public static final String KEY_DIASTOLIC = "diastolic";
	public static final String KEY_PULSE = "pulse";
	public static final String KEY_PULSE_STATE = "pulse_state";
	public static final String KEY_LEVEL = "level";
	public static final String KEY_TIME_STR = "time_str";
	public static final String KEY_LATITUDE = "latitude";
	public static final String KEY_LONGITUDE = "longitude";
	public static final String KEY_NOTE = "note";
	public static final String KEY_MEASUREMENT = "measurement";
	public static final String KEY_IS_NEW = "is_new";
	public static final String KEY_IS_READ = "is_read";

	public static final String MESSAGE_TABLE_CREATE = "CREATE TABLE "
			+ MESSAGE_TABLE + " ("
			+ KEY_MESSAGE_ID + " VARCHAR PRIMARY KEY, "
			+ KEY_ATTENDEE + " VARCHAR, "
			+ KEY_ATTENDEE_UID + " INTEGER, "
			+ KEY_MESSAGE + " TEXT, "
			+ KEY_SYSTOLIC + " INTEGER, "
			+ KEY_DIASTOLIC + " INTEGER, "
			+ KEY_PULSE + " INTEGER, "
			+ KEY_PULSE_STATE + " TINYINT, "
			+ KEY_LEVEL + " TINYINT, "
			+ KEY_TIME_STR + " VARCHAR, "
			+ KEY_LATITUDE + " DOUBLE, "
			+ KEY_LONGITUDE + " DOUBLE, "
			+ KEY_NOTE + " TEXT, "
			+ KEY_MEASUREMENT + " INTEGER, "
			+ KEY_IS_NEW + " BOOLEAN, "
			+ KEY_IS_READ + " BOOLEAN);";

	public long insertMessage(MeasureMessageEntity messageEntity) {
		return DBHelper.getInstance().getDatabase().insert(MESSAGE_TABLE, null, createContentValues(messageEntity));
	}

	public boolean updateRecord(MeasureMessageEntity messageEntity) {
		return DBHelper.getInstance().getDatabase().update(MESSAGE_TABLE, createContentValues(messageEntity), KEY_MESSAGE_ID + "=" + messageEntity.messageId, null) > 0;
	}

	public boolean deleteRecord(int messageId) {
		return DBHelper.getInstance().getDatabase().delete(MESSAGE_TABLE, KEY_MESSAGE_ID + "=" + messageId, null) > 0;
	}

	public List<MeasureMessageEntity> getAllMessage(){return getAllMessage(null,null);}
	public List<MeasureMessageEntity> getNewMessages(){return getAllMessage(KEY_IS_NEW + " = ?",new String[]{"TRUE"});}
	public List<MeasureMessageEntity> getAttendeeMessages(String attendee){return getAllMessage(KEY_ATTENDEE + " = ?",new String[]{attendee});}
	public List<MeasureMessageEntity> getAllMessage(String selection,String[] selectionArgs) {
		Cursor cursor = DBHelper.getInstance().getDatabase().query(MESSAGE_TABLE,null,	selection, selectionArgs, null, null, null);
		List<MeasureMessageEntity> messageEntityList = new ArrayList<>();
		while (cursor != null && cursor.moveToNext()) {
			messageEntityList.add(getMessageByCursor(cursor));
		}
		cursor.close();
		return messageEntityList;
	}

	public MeasureMessageEntity getMessageByCursor(Cursor cursor){
		MeasureMessageEntity messageEntity = new MeasureMessageEntity();
		messageEntity.messageId = cursor.getInt(cursor.getColumnIndex(KEY_MESSAGE_ID));
		messageEntity.attendee = cursor.getString(cursor.getColumnIndex(KEY_ATTENDEE));
		messageEntity.attendee_uid = cursor.getInt(cursor.getColumnIndex(KEY_ATTENDEE_UID));
		messageEntity.message = cursor.getString(cursor.getColumnIndex(KEY_MESSAGE));
		messageEntity.systolic = cursor.getInt(cursor.getColumnIndex(KEY_SYSTOLIC));
		messageEntity.diastolic = cursor.getInt(cursor.getColumnIndex(KEY_DIASTOLIC));
		messageEntity.pulse = cursor.getInt(cursor.getColumnIndex(KEY_PULSE));
		messageEntity.pulsestate = cursor.getString(cursor.getColumnIndex(KEY_PULSE_STATE));
		messageEntity.level = cursor.getInt(cursor.getColumnIndex(KEY_LEVEL));
		messageEntity.timeStr = cursor.getString(cursor.getColumnIndex(KEY_TIME_STR));
		messageEntity.latitude = cursor.getDouble(cursor.getColumnIndex(KEY_LATITUDE));
		messageEntity.longitude = cursor.getDouble(cursor.getColumnIndex(KEY_LONGITUDE));
		messageEntity.note = cursor.getString(cursor.getColumnIndex(KEY_NOTE));
		messageEntity.measurement = cursor.getInt(cursor.getColumnIndex(KEY_MEASUREMENT));
		messageEntity.isNew = cursor.getInt(cursor.getColumnIndex(KEY_IS_NEW)) == 1;
		messageEntity.isRead = cursor.getInt(cursor.getColumnIndex(KEY_IS_READ)) == 1;
		return messageEntity;
	}

	public RecordEntity getRecord(int messageId) throws SQLException {
		Cursor cursor = DBHelper.getInstance().getDatabase().rawQuery("SELECT * FROM "+ MESSAGE_TABLE + " WHERE "+ KEY_MESSAGE_ID +" = " + messageId,null);
		if (cursor != null && cursor.moveToFirst()) {
			getMessageByCursor(cursor);
		}
		return null;
	}

	private ContentValues createContentValues(MeasureMessageEntity messageEntity) {
		ContentValues values = new ContentValues();
		values.put(KEY_MESSAGE_ID,messageEntity.messageId);
		values.put(KEY_ATTENDEE,messageEntity.attendee);
		values.put(KEY_ATTENDEE_UID,messageEntity.level);
		values.put(KEY_MESSAGE,messageEntity.message);
		values.put(KEY_SYSTOLIC,messageEntity.systolic);
		values.put(KEY_DIASTOLIC,messageEntity.diastolic);
		values.put(KEY_PULSE,messageEntity.pulse);
		values.put(KEY_PULSE_STATE,messageEntity.pulsestate);
		values.put(KEY_LEVEL,messageEntity.level);
		values.put(KEY_TIME_STR,messageEntity.timeStr);
		values.put(KEY_LATITUDE,messageEntity.latitude);
		values.put(KEY_LONGITUDE,messageEntity.longitude);
		values.put(KEY_NOTE,messageEntity.note);
		values.put(KEY_MEASUREMENT,messageEntity.measurement);
		values.put(KEY_IS_NEW,messageEntity.isNew);
		values.put(KEY_IS_READ,messageEntity.isRead);
		return values;
	}
}
